package com.yicong.ants.ui.video.adapter;

import static com.yicong.ants.utils.UIUtils.showToast;

import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.cchao.simplelib.core.RxHelper;
import com.cchao.simplelib.core.UiHelper;
import com.cchao.simplelib.ui.adapter.StatefulBindQuickAdapter;
import com.cchao.simplelib.ui.interfaces.BaseView;
import com.kennyc.view.MultiStateView;
import com.yicong.ants.BR;
import com.yicong.ants.Constants;
import com.yicong.ants.R;
import com.yicong.ants.bean.video.FocusBean;
import com.yicong.ants.manager.BusinessHelper;
import com.yicong.ants.manager.UserManager;
import com.yicong.ants.manager.http.RetrofitHelper;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.disposables.CompositeDisposable;

/**
 * 关注列表
 *
 * @author LinDingQiang
 * @description
 * @time 4/21/21 2:08 PM
 * @email dingqiang.l
 */
public class FocusListAdapter extends StatefulBindQuickAdapter<FocusBean> {
    CompositeDisposable mDisposable;
    String uid;
    BaseView mBaseView;

    public FocusListAdapter(BaseView baseView, CompositeDisposable disposable, String uid) {
        super(R.layout.focus_list_item);
        this.mBaseView = baseView;
        mDisposable = disposable;
        this.uid = uid;
    }

    @Override
    protected void convert(DataBindViewHolder helper, FocusBean item) {
        helper.getBinding().setVariable(BR.bean, item);
        Button btnFocus = helper.getBinding().getRoot().findViewById(R.id.btn_focus);
        btnFocus.setBackgroundResource(item.isFocused() ? R.drawable.shape_focus_list_btn_normal : R.drawable.shape_focus_list_btn_pre);
        btnFocus.setText(item.isFocused() ? "已关注" : "取消关注");
        helper.getBinding().setVariable(BR.click, (View.OnClickListener) v -> {
            if (UserManager.notLogin()) {
                UiHelper.showToast("请先登录");
                return;
            }

            Map<String, String> focusMap = new HashMap<>();
            focusMap.put("author_uid", item.getFocus_id());
            TextView textView = (TextView) v;

            mDisposable.add(RetrofitHelper.getApis().videoFocus(focusMap)
                .compose(RxHelper.toMain())
                .subscribe(respBean -> {
                    if (respBean.isCodeFail()) {
                        UiHelper.showToast(respBean.getMsg());
                        return;
                    }
                    item.setIs_focus(item.isFocused() ? 0 : 1);
                    textView.setBackgroundResource(item.isFocused() ? R.drawable.shape_focus_list_btn_normal : R.drawable.shape_focus_list_btn_pre);
                    textView.setText(item.isFocused() ? "已关注" : "取消关注");
                    UiHelper.showToast(item.isFocused() ? "已关注" : "取消关注");
                    BusinessHelper.putFollow(item.getFocus_id(), item.getIs_focus() == 1);
                }, RxHelper.getHideProgressConsumer(mBaseView)));
        });
    }

    @Override
    public void loadPageData(int page) {
        final Map<String, String> params = new HashMap<>();
        params.put("page", String.valueOf(page));
        params.put("page_size", String.valueOf(Constants.PAGE_SIZE));
        params.put("uid", uid);

        if (page == 1) {
            setViewState(MultiStateView.VIEW_STATE_LOADING);
        }
        mDisposable.add(RetrofitHelper.getApis().getFocusList(params)
            .compose(RxHelper.toMain())
            .subscribe(respBean -> {
                if (respBean.isCodeFail()) {
                    showToast(respBean.getMsg());
                    if (page == 1) {
                        setViewState(MultiStateView.VIEW_STATE_ERROR);
                    }
                    return;
                }

                solveData(respBean.getData(), page, Constants.PAGE_SIZE);
            }, RxHelper.getSwitchableErrorConsumer(this)));
    }
}
